/* StringExtension - Module that implements all kind of string functions */

#include "stdafx.h"
#include "StringExtension.h"

/*********************************************************************************************************************** */
/* Description: Removes spaces and tabs from the beginning and the end of a string */
/* Input/Output: str - string to trim */
/*********************************************************************************************************************** */
void trim(char* str)
{
    if(!str)
        return;

    char* ptr = str;
    int len = strlen(ptr);

    while(len-1 > 0 && isspace(ptr[len-1]))
        ptr[--len] = 0;

    while(*ptr && isspace(*ptr))
        ++ptr, --len;

    memmove(str, ptr, len + 1);
}

/*********************************************************************************************************************** */
/* Description: Removes spaces and tabs from the beginning and the end of a string */
/* Input/Output: str - string to trim */
/* Input/Output: len - trim text only in scope of the length */
/*********************************************************************************************************************** */
int trim(char* str, int len)
{
    if(!str)
        return 0;

    char* ptr = str;

    while(len-1 > 0 && isspace(ptr[len-1]))
        ptr[--len] = 0;

    while(*ptr && isspace(*ptr))
        ++ptr, --len;

    memmove(str, ptr, len + 1);

	return len;
}

/*********************************************************************************************************************** */
/* Description: Clear spaces and tabs  in the string and not only in the begin and end */
/* Input/Output: str - string to clear */
/* Output: cleared string */
/*********************************************************************************************************************** */
char* ClearSpacesAndTabs(char* str)
{
	int i;
	int bufferIndex = 0;
	int len = strlen(str);
	char* buffer = (char*)malloc(len + 1);

	for (i=0;i<len;i++)
	{
		if (!isspace(str[i]))
		{
			buffer[bufferIndex] = str[i];
			bufferIndex++;
		}
	}

	buffer[bufferIndex] = '\0';
	memcpy(str, buffer, bufferIndex+1);

	free(buffer);

	return str;
}

/*********************************************************************************************************************** */
/* Description: Search a character in a string and copy the string after that charcter */
/* Input: text - string to search in */
/* charToSearch - Char to search */
/* Output: Copied string - dynamic memory allocation */
/*********************************************************************************************************************** */
char* SearchWithCpoy(char* text, char charToSearch)
{
	char *found;
	char *attrVal = NULL;

	found = strchr(text, charToSearch);

	if (found)
	{
		size_t len = found - text;

		attrVal = (char*)malloc(len + 1);
		memcpy(attrVal, text, len);
		attrVal[len] = '\0';
	}

	return attrVal;
}

/*********************************************************************************************************************** */
/* Description: Search a space in a string */
/* Input: text - string to search in */
/* Output: pointer to the space */
char* SearchSpace(char* text)
{
	char *foundSpace;
	char *foundTab;

	foundSpace = strchr(text, ' ');
	foundTab = strchr(text, '\t');

	if (!foundSpace && !foundTab)
		return foundSpace;

	if (foundSpace && !foundTab)
		return foundSpace;

	if (!foundSpace && foundTab)
		return foundTab;

	if (foundSpace > foundTab)
		return foundTab;

	return foundSpace;
}

/*********************************************************************************************************************** */
/* Description: Check if a character is a number or an alphabeti character */
/* Input: c - character */
/* Output: boolean */
/*********************************************************************************************************************** */
BOOL CheckAlphanumericChar(char c)
{
	return (c >= '0' && c <= '9') ||
			(c >= 'a' && c <= 'z') ||
			(c >= 'A' && c <= 'Z');
}
/*********************************************************************************************************************** */
/* Description: Check if a string contains only numbers or alphabeti characters */
/* Input: text - text */
/* Output: boolean */
/*********************************************************************************************************************** */
BOOL CheckAlphanumericString(char* text)
{
	int len = strlen(text);
	int i;
	
	for (i=0;i<len;i++)
	{
		if (!CheckAlphanumericChar(text[i]))
			return FALSE;
	}
	
	return TRUE;
}

/*********************************************************************************************************************** */
/* Description: Check validation of the number */
/* Input: str - string to convert */
/* Output: True - valid integer. False - not valid. */
/*********************************************************************************************************************** */
BOOL isValidInt(const char *str)
{
   /* Handle negative numbers. */
   if (*str == '-' || *str == '+')
      ++str;

   /* Handle empty string or just "-". */
   if (!*str)
      return FALSE;

   /* Check for non-digit chars in the rest of the stirng. */
   while (*str)
   {
      if (!isdigit(*str))
         return FALSE;
      else
         ++str;
   }

   return TRUE;
}
/*********************************************************************************************************************** */
/* Description: Transfor string to integer, check validation of the number*/
/* Input: str - string to convert */
/* Output: True - valid integer. False - not valid. */
/*********************************************************************************************************************** */
BOOL StringToInteger(char* str, int* number)
{
	if (!isValidInt(str))
		return FALSE;
	
	*number = atoi(str);
	return TRUE;
}


/*********************************************************************************************************************** */
/* Description: Search a string in string array */
/* Input: stringsArray - string array */
/* Input: arraySize - Size of the array */
/* Input: cellSize - Size of each cell in the array */
/* Input: text - text to find */
/*********************************************************************************************************************** */
int GetPosOfStringInArray(char* stringsArray, int arraySize, int cellSize, char* text)
{
	int i;
	for (i=0; i< arraySize; i++)
	{
		if (strcmp(stringsArray+i*cellSize, text) == 0)
			return i;
	}

	return NOT_FOUND;
}

/*********************************************************************************************************************** */
/* Description: Check if the string contains characters other then spaces and tabs */
/* Input: str - string */
/* Input: len - size to check */
/*********************************************************************************************************************** */
BOOL IsNotEmpty(char* str, int len)
{
	int i;
	for (i=0; i< len; i++)
	{
		if (*(str+i) != ' ' && *(str+i) != '\t')
			return TRUE;
	}

	return FALSE;
}
